package binaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
//统计完全二叉树节点的个数
public class WanquanTree {

    //使用层序遍历
    public int countNodes(TreeNode root) {
        int num=0;
        LinkedList<TreeNode> list = new LinkedList<>();
        if (root!=null){
            list.add(root);
            num++;
        }
        while (list.size()>0){
            int size = list.size();
            while (size>0){
                TreeNode cur = list.poll();
                if (cur.left!=null){
                   list.add(cur.left);
                   num++;
                }
                if (cur.right!=null){
                    list.add(cur.right);
                    num++;
                }
                size--;
            }
        }
        return num;
    }


//递归实现
       public static  int num=0;
     public   int  countNodes1(TreeNode  root){
         if (root==null){
             return  0;
         }
         num++;
         countNodes1(root.left);
        countNodes1(root.right);
        return  num;
     }



    public static void main(String[] args) {
        TreeNode root= new TreeNode(5);
//        TreeNode treeNode1 = new TreeNode(3);
//        TreeNode treeNode2=new TreeNode(3);
//        TreeNode treeNode3 = new TreeNode(3);
//        TreeNode treeNode4= new TreeNode(3);
//        root.left=treeNode1;
//        root.right=treeNode3;
//        treeNode1.left=treeNode2;
//        treeNode3.right=treeNode4;

        WanquanTree wanquanTree = new WanquanTree();

        System.out.println(wanquanTree.countNodes1(root));



    }




}
